package xcmg.device.service.prepPay;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.company.CompanyDealerDO;
import xcmg.device.dao.entity.prepPay.PrepPayDO;
import xcmg.device.dao.mapper.CompanyDealerMapper;
import xcmg.device.dao.mapper.prepPay.PrepPayDOMapper;
import xcmg.device.infra.ExportUtil;
import xcmg.device.infra.enums.PrepPayBillTypeEnum;
import xcmg.device.infra.enums.WhetherTypeEnum;
import xcmg.device.service.vo.prepPay.PrepPayQueryVO;
import yb.ecp.fast.infra.util.PageHelperPlus;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiali@bkrwin.com
 * @description: 应收-预付款金额详情Service
 * @date： 2019/6/12 15:10
 **/
@Service
public class PrepPayDetailService {
    @Autowired
    PrepPayDOMapper prepPayDOMapper;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    CompanyDealerMapper companyDealerMapper;


    private static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void export(List<String> ids)throws Exception{
        PageHelperPlus.orderBy("create_date desc");
        invokeExport(prepPayDOMapper.findByIds(ids));
    }

    public void exportAll(PrepPayQueryVO condition)throws Exception{
        CompanyDealerDO companyDealerDO =companyDealerMapper.itemByOrgId(condition.getOrgId(),
                condition.getCustomerId());
        if (companyDealerDO==null){
            return ;
        }
        condition.setCustomerId(String.valueOf(companyDealerDO.getId()));
        Integer count = prepPayDOMapper.listCount(condition);
        List<PrepPayDO> dataList = new ArrayList<>();
        int times = count/500+1;
        for(int i=0; i<times; i++){
            PageHelperPlus.startPage(i + 1, 500, "create_date desc");
            dataList.addAll(prepPayDOMapper.getListByQueryVO(condition));
        }
        invokeExport(dataList);
    }

    private void invokeExport(List<PrepPayDO> doList)throws Exception{
        String title = "预付款金额详情数据导出";
        String[] rowsName = new String[]{"序号", "客户名称", "单据类型","单据编号", "交易金额", "原金额","交易后金额", "创建时间", "是否红冲", "红冲时间", "充值备注", "红冲备注"};
        List<Object[]>  dataList = new ArrayList<Object[]>();
        Object[] objs;
        for (int i = 0; i < doList.size(); i++) {
            PrepPayDO exportDO = doList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i+1;
            objs[1] = exportDO.getCustomerName();
            if (exportDO.getBillType()==0){
                objs[2]= PrepPayBillTypeEnum.RECHARGE.getStatusName();
            }else if (exportDO.getBillType()==1){
                objs[2]= PrepPayBillTypeEnum.SETTLEMENT.getStatusName();
            }else{
                objs[2]= PrepPayBillTypeEnum.RETURN.getStatusName();
            }
            objs[3] = exportDO.getBillNo();
            objs[4] = exportDO.getTransactionAmount();
            BigDecimal totalAmount = exportDO.getTotalAmount() == null ?BigDecimal.ZERO:exportDO.getTotalAmount();
            BigDecimal transactionAmount = exportDO.getTransactionAmount() == null ?BigDecimal.ZERO:exportDO.getTransactionAmount();
            objs[5] = totalAmount.subtract(transactionAmount);
            objs[6] = exportDO.getTotalAmount();
            objs[7] = format.format(exportDO.getCreateDate());
            objs[8] = exportDO.getDiscardFlag()==0 ? WhetherTypeEnum.NoFlag.getDesc():WhetherTypeEnum.YesFlag.getDesc();
            if(exportDO.getDiscardFlag() == 0 ){
                objs[9] = "";
            }else {
                objs[9] = format.format(exportDO.getDiscardDate());
            }
            objs[10] = exportDO.getRemark();
            objs[11] = exportDO.getDiscardRemark();
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }
}
